<?php
/**
 * 脚本名：mysql.class.php
 * 说明：mysql操作类
 */
class MysqlDriver {
	private $conn = null;
	private $tag = "Mysql";

	public function __construct() {

	}

	public function connect($c) {
		if(!isset($c['port'])){
			$c['port'] = '3306';
		}
		$server = $c['host'] . ':' . $c['port'];
		$this->conn = mysql_connect($server, $c['username'], $c['password'], true);
		if ($this->conn) {
			$ret = mysql_select_db($c['dbname'], $this->conn);
			if (!$ret) {
				return false;
			}
			if ($c['charset']) {
				return mysql_query("set names " . $c['charset'], $this->conn);
			}
			return true;
		}
		return false;
	}

	public function close() {
		return mysql_close($this->conn);
	}

	public function find($sql) {
		$data = array();

		$result = mysql_query($sql, $this->conn);
		if ($result) {
			while ($row = mysql_fetch_assoc($result)) {
				$data[] = $row;
			}
		}
		return $data;
	}

	public function select($table, $columns, $where, $other = '') {
		$cond = '';
		foreach ($where as $k => $v) {
			$value = mysql_real_escape_string($v);
			$cond .= "$k = '$value' AND ";
		}
		$cond = substr($cond, 0, strlen($cond) - 5);

		$sql = "SELECT $columns FROM {$table} WHERE $cond $other";


		file_put_contents('ttr.txt',$sql);;
		$data = array();
			
		$result = mysql_query($sql, $this->conn);
		if ($result) {
			while ($row = mysql_fetch_assoc($result)) {
				$data[] = $row;
			}
		}
		return $data;
	}

	
	public function insert($table,$array){
		$temp="";$temp2='';
		foreach($array as $key=>$value){
			$temp .="$key,";$temp2 .="'$value',";
		}
		$temp = substr($temp,0,strlen($temp)-1);
		$temp2 = substr($temp2,0,strlen($temp2)-1);
		$sql = "INSERT INTO $table ($temp) VALUE($temp2)";
		mysql_query($sql, $this->conn);
		if (!mysql_insert_id()) die ( mysql_error());
		return mysql_insert_id();
	}
	
	public function update($table, $row, $where) {
		$stat = '';
		foreach ($row as $k => $v) {
			$value = mysql_real_escape_string($v);
			$stat .= "`$k` = '$value',";
		}
		$stat = substr($stat, 0, strlen($stat) - 1);

		$cond = '';
		foreach ($where as $k => $v) {
			$value = mysql_real_escape_string($v);
			$cond .= "`$k` = '$value' AND ";
		}
		$cond = substr($cond, 0, strlen($cond) - 5);

		$sql = "UPDATE `{$table}` SET $stat where $cond";

		return  mysql_query($sql, $this->conn);
	}

	public function insert_or_update($table, $row) {

		$stat = '';
		foreach ($row as $k => $v) {
			$value = mysql_real_escape_string($v);
			$stat .= "`$k` = '$value',";
		}

		$stat = substr($stat, 0, strlen($stat) - 1);
		$sql = "INSERT INTO `{$table}` SET $stat ON DUPLICATE KEY UPDATE $stat";

		mysql_query($sql, $this->conn);
	}

	public function query($sql) {
		//file_put_contents('sql.txt',$sql);
		return mysql_query($sql, $this->conn);
	}
}

?>
